<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Site extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->lang->load('sites');
		$this->load->model(['site_model','goods_model','siteproduct_model','siteorder_model']);
		$this->data['page_home']  = lang('lang_dashboard');
		$this->data['page_title'] = lang('lang_site');
		$this->data['sitetype'] = $this->config->item('site_types');
				
	}


	public function testprice()
	{
		$data=[];
		$price=$this->input->post("price");
		$ruler=$this->input->post("ruler");
		$data["price"]=get_price($ruler,$price);
		echo json_encode($data);
		die();
	}
	
	public function index()
	{

		// Title
		$this->data['meta_title'] = $this->data['page_title'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home']  => 'backend',
			$this->data['page_title'] => 'backend/products'
		);

		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
	

                $newdata=[
                    "name"=>$this->input->post("name"),
                    "domian"=>$this->input->post("domian"),
                    "sitetype"=>$this->input->post("sitetype"),
					"apipass"=>$this->input->post("apipass"),
					"memo"=>$this->input->post("memo"),
				];

                $this->site_model->create($newdata);
                redirect('backend/sites/site', 'refresh');
                die();
            }
		}

		$page=max(1,(int) $this->input->get('page'));
		$key=trim($this->input->get('keyword'));
		$sitetype=trim($this->input->get('sitetype'));
		$trash=trim($this->input->get('trash'));
		
		$page_data=$this->site_model->getpage($page,$key,$sitetype,$trash);
	
		$this->data['trash']=$trash;
        $this->data['data']=$page_data["data"];
        $this->data['pager']=$page_data["pager"];
		$this->data['total']=$page_data["total"];
        $this->data['pages']=$page_data["pages"];
		$this->data['page']=$page;    
		
		$keyword = array(
				'name'      => 'keyword',
				'id'        => 'keyword',
				'value'     =>  $key,
				'class'     =>  'form-control input-sm',
				'placeholder'=> '请输入关键字',
				'maxlength' => '100'
		);

		$this->data['keyword']=$keyword;
		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'sites/index';

		$this->render();
	}

	public function modify($siteid)
	{
		$site=$this->site_model->getitembyid($siteid);
		if(!$site) die('error link');
		
		if(isset($site["extra"]) && $site["extra"])
		{
			$site["extra"]=json_decode($site["extra"],true);
			if(!isset($site["extra"]["merchid"]))
			{
				$site["extra"]["merchid"]="";
			}
			if(!isset($site["extra"]["apiauth"]))
			{
				$site["extra"]["apiauth"]="";
			}
		}
		else
		{
			$site["extra"]=["merchid"=>"","apiauth"=>""];
		}

		$this->data["site"]=$site;
		$this->data['content'] = 'sites/modify';    
		$this->clean_render();
	}

    public function ajaxedit()
    {
        $attrName=$this->input->post("attrName");
		$newHtml=$this->input->post("newHtml");
		if(!in_array($attrName,['name','domian','email','memo','sku','ruler','emailpass','issync','pinterest','feed'])) die();
        $cid=$this->input->post("cid");
        $newdata[$attrName]=$newHtml;
        $this->site_model->update($cid, $newdata);
        echo json_encode([]);
        die();
	}

	public function ajaxextraedit()
    {
        $attrName=$this->input->post("attrName");
		$newHtml=$this->input->post("newHtml");		
		$cid=$this->input->post("cid");
		$site=$this->site_model->getitembyid($cid);
		if($site["extra"])
		{
			$site["extra"]=json_decode($site["extra"],true);
		}
		else
		{
			$site["extra"]=[];
		}
		$site["extra"][$attrName]=$newHtml;
        $newdata["extra"]=json_encode($site["extra"]);
        $this->site_model->update($cid, $newdata);
        echo json_encode([]);
        die();
	}
	
}
